﻿using LJ.Abstractions.Exceptions;
using LJ.FaultHandler.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJ.FaultHandler.Polly.Internal;

public class PollyFaultHandlerFactory(IServiceProvider serviceProvider) : IFaultHandlerFactory
{
    private readonly IServiceProvider _serviceProvider = serviceProvider;

    public IFaultHandler Create(string? key = null)
    {
        var faultHandler = _serviceProvider.GetRequiredService<IFaultHandler>();

        if (faultHandler is not PollyFaultHandler pollyFaultHandler)
        {
            throw new AppException($"{GetType().FullName} IFaultHandler is not {nameof(PollyFaultHandler)}");
        }
        if (key != null)
        {
            pollyFaultHandler.SetAppKey(key);
        }
        return pollyFaultHandler;
    }
}
